package com.bobo.controller.back.category;

import com.bobo.annotations.RequestBody;
import com.bobo.annotations.RequestMapping;
import com.bobo.annotations.RequestMethod;
import com.bobo.controller.base.BaseServlet;
import com.bobo.model.Category;
import com.bobo.model.Photo;
import com.bobo.model.Tag;
import com.bobo.service.category.CategoryServiceImpl;
import com.bobo.service.category.ICategoryService;
import com.bobo.service.photo.IPhotoService;
import com.bobo.service.photo.PhotoServiceImpl;
import com.bobo.service.tag.ITagService;
import com.bobo.service.tag.TagServiceImpl;
import com.bobo.util.UploadFiles;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by bobo on 16-10-27.
 */
@WebServlet(name = "category",urlPatterns = {"/admin/category/*"})
public class CategoryController extends BaseServlet{

    IPhotoService photoService = new PhotoServiceImpl();
    UploadFiles uploadFiles = new UploadFiles();
    ICategoryService categoryService = new CategoryServiceImpl();
    ITagService tagService = new TagServiceImpl();
    @RequestMapping("/add")
    public String add1(){
        return "jsp/back/category/add";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(HttpServletRequest request, HttpServletResponse response){
        try {
            Map<String, Object[]> map = uploadFiles.upload(request);
            Category category = new Category();

            Object[] obj = map.get("fileName");
            Object[] names = map.get("name");

            if(names !=null &&names.length>0){
                category.setName((String)names[0]);
            }else {
                return "error";
            }
            if (obj !=null && obj.length > 0) {
                Photo photo = new Photo();
                photo.setImg((String) obj[0]);
                photo.setStatus(2);
                Integer pid = photoService.create(photo);
                Photo photo1 = photoService.getPhoto(pid);
                category.setPhoto(photo1);
            }else {
                return "error";
            }
            categoryService.create(category);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:admin/category/getCategories";
    }

    @RequestBody
    @RequestMapping("/del")
    public String del(HttpServletRequest request){
        int id = Integer.parseInt(request.getParameter("id"));
        Category category = categoryService.getById(id);
        Photo photo = category.getPhoto();
        boolean delete = categoryService.delete(id);
        if(delete){
            uploadFiles.delFile(request,photo.getImg());
            photoService.delete(photo.getId());
            return "true";
        }else {
            return "false";
        }
    }

    @RequestMapping(value = "/upd",method = RequestMethod.POST)
    public String updd(HttpServletRequest request, HttpServletResponse response){
        try {
            Map<String, Object[]> map = uploadFiles.upload(request);
            Category category;
            Object[] obj = map.get("fileName");
            Object[] ids = map.get("id");
            Object[] names = map.get("name");
            Object[] statuses = map.get("status");


            if(ids!=null&&ids.length>0){
                String id = (String) ids[0];
                category = categoryService.getById(Integer.valueOf(id));
                if (obj!=null && obj.length > 0) {
                    Photo photo = category.getPhoto();
                    uploadFiles.delFile(request,photo.getImg());
                    photo.setImg((String) obj[0]);
                    photoService.update(photo);
                    category.setPhoto(photo);
                }

                if(names!=null&&names.length>0){
                    category.setName((String) names[0]);
                }else {
                    return "error";
                }
                if(statuses!=null&&statuses.length>0){
                    String status = (String) statuses[0];
                    category.setStatus(Integer.valueOf(status));
                }else {
                    return "error";
                }
                categoryService.update(category);
            }

         } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:/admin/category/getCategories";
    }

    @RequestMapping("/upd")
    public String upd(HttpServletRequest request){
        int id = Integer.parseInt(request.getParameter("id"));
        Category category = categoryService.getById(id);
        request.setAttribute("category",category);
        return "jsp/back/category/upd";
    }

    @RequestMapping("/get")
    public String get(HttpServletRequest request){
        int id = Integer.parseInt(request.getParameter("id"));
        Category category = categoryService.getById(id);
        List<Tag> tags = tagService.getTags(id, null);
        category.setTags(tags);
        request.setAttribute("category",category);
        return "jsp/back/category/get";
    }

    @RequestMapping("/getCategories")
    public String getAdmins(HttpServletRequest request) {
//        Admin admin = (Admin) request.getSession().getAttribute("admin");
//        if (admin == null || admin.getLevel() != 3 || admin.getLevel() != 0) {
//            return "redirect:/back";
//        } else {
            List<Category> categories = categoryService.getCategories(null);
            request.setAttribute("categories", categories);
            return "jsp/back/category/getCategories";
//        }
    }
    @RequestBody
    @RequestMapping("/changeStatus")
    public String changeStatus(HttpServletRequest request){
        int id = Integer.parseInt(request.getParameter("id"));
        Category category = categoryService.getById(id);
        int status = Integer.parseInt(request.getParameter("status"));
        category.setStatus(status);
        categoryService.update(category);
        return "true";
    }
}
